package com.sky.controller.admin;



import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;

import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;


    //--1--新增菜品
    @PostMapping()
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品，参数为：{}", dishDTO);
        dishService.saveOne(dishDTO);
        //清缓存
        cleanCache("dish_"+dishDTO.getCategoryId());
        return Result.success();
    }


    //--2--条件分页查询  --K-V参数
    @GetMapping("/page")
    @ApiOperation("条件分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("条件分页查询，参数为：{}", dishPageQueryDTO);
        PageResult pageResult=dishService.selectPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    //--3--批量删除  --K-V参数
    @DeleteMapping()
    @ApiOperation("批量删除")
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("批量删除，参数为：{}", ids);
        dishService.deleteByIds(ids);
        //清缓存
        cleanCache("dish_*");
        return Result.success();
    }

    //--4--根据id查菜品（包含口味）
    @GetMapping("{id}")
    @ApiOperation("根据id查菜品")
    public Result<DishDTO> getDishById(@PathVariable Long id){
        log.info("根据id查菜品，参数为：{}", id);
        DishDTO dishDTO=dishService.getDishById(id);
        return Result.success(dishDTO);
    }

    //--4--修改菜品
    @PutMapping()
    @ApiOperation("修改菜品")
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品，参数为：{}", dishDTO);
        dishService.updateDish(dishDTO);

        //清缓存
        cleanCache("dish_*");
        return Result.success();
    }


    //--5--停售/起售菜品  /admin/dish/status/{status}
    @PostMapping("status/{status}")
    @ApiOperation("停售/起售菜品 ")
    public Result updateDishStatus(@PathVariable Integer status,Long id){
        log.info("停售/起售菜品，参数为：{}", status);
        dishService.updateDishStatus(status,id);
        //清缓存
        cleanCache("dish_*");
        return Result.success();
    }

    //--7--根据分类id查询菜品  http://localhost/api/dish/list?categoryId=11
    @GetMapping("list")
    @ApiOperation("根据分类id查询菜品 ")
    public Result selectDishByCategoryId(Long categoryId){
        log.info("根据分类id查询菜品，参数为：{}", categoryId);
        List<DishVO> dishList=dishService.selectDishByCategoryId(categoryId);
        return Result.success(dishList);
    }


    //--清除缓存
    private void cleanCache(String keyPattern) {
        log.info("[清除缓存：{keyPattern}]", keyPattern);
        if(keyPattern=="dish_*"){
            Set delkeys = redisTemplate.keys(keyPattern);
            redisTemplate.delete(delkeys);    //所有
        }else {
            redisTemplate.delete(keyPattern); //单个
        }

    }
}

